<?php
/**
 * 
 *Ogheneovo Dibie
 *This Controller deals with the addition, edit and deletion of courses 
 *in the course database.
 */
class Admin_CoursesController extends Zend_Controller_Action
{

	protected $course;
    public function init()
    {
        /* Initialize action controller here */
    	$this->course = new Admin_Model_DbTable_Courses();
    	$this->dept = $this->_getParam('dept');
    }

    /**
     * Index Action
     * This method allows for the addition of a particular course to the database.
     */
   	 public function indexAction()
    {
        // action body
         $form = new Admin_Form_addCourse();
        $this->view->form = $form;
        $this->view->title = "Add a course to the database";
        $this->view->headtitle($this->view->title);
      	$addCourse = new Admin_Model_DbTable_Courses();
        
        if($this->getRequest()->isPost()) {
        $formData = $this->getRequest()->getPost();
		if ($form->isValid($formData))      {
			
			$cname = $form->getValue('courseName');
			$cnumber = $form->getValue('courseNo');
			$school = $form->getValue('school');
			$dept = $form->getValue('dept');
			$category = $form->getValue('cat');
			$recs =$addCourse->checkSubmission($cnumber);
			$count = $recs[0]['val'];
			if ($count < 1){
			$addCourse->insertCourse($cname, $cnumber,$dept, $category,$school);
			$this->view->message = "Added";
			}else{
				$this->view->message= "Error: A course with name, $cnumber already exists in the database";
			}
			}  
        }
        
    }


    /**
     * Function: editAction
     * This function allows a user edit a particular course in the database
     */
     public function viewschoolsAction() {
        // action body
        $recs = new Community_Model_DbTable_coursedetails();
        $list = $recs->getDistinctSchools();
        //print_r($list);
       /*Pagination control on the page
        $paginator = new Zend_Paginator(new Zend_Paginator_Adapter_DbSelect($list));
        $paginator->setItemCountPerPage(10)
        		  ->setCurrentPageNumber($this->_getParam('page', 1));
       
      $this->view->paginator = $paginator;    */
       $this->pagination($list, 10);
     }
     
     

		public function getdeptsAction() {
			$id = $this->_getParam('school');
			$depts = new Community_Model_DbTable_coursedetails();
			$results = $depts->getschooldepts($id);
			$this->pagination($results, 10);
		}
		

		public function getcoursesAction() {
		$id = $this->dept;
		$sid = $this->_getParam('school');
		$courses = new Community_Model_DbTable_coursedetails();
		$results = $courses->getcourses($id, $sid);
		$this->pagination($results, 5);
		}
		
		
		/**
		 * function: Delete Course
		 */

		public function deleteAction() {
			
			$id = $this->_getParam('id');
			$sid = $this->_getParam('school');
			$this->course->deleteCourse($id);
			$this->_forward('getcourses', 'courses', 'admin', array('school'=>$sid, 'dept'=>$this->dept));
		}
		
		public	function editAction(){
				$this->view->title = "Edit this Course";
				$this->view->headTitle($this->view->title, 'PREPEND');
				$form = new Admin_Form_addCourse();
				$form->submit->setLabel('Save');
				$this->view->form = $form;
				if ($this->getRequest()->isPost()) {
				$formData = $this->getRequest()->getPost();
				if ($form->isValid($formData)) {
				$id =$this->_getParam('id');
				$sid = $this->_getParam('school');
				$deptid = $this->_getParam('dept');
				$school = $form->getValue('school');
				$dept = $form->getValue('dept');
				$cat = $form->getValue('cat');
				$courseNo = $form->getValue('courseNo');
				$courseName = $form->getValue('courseName');
				$albums = new Admin_Model_DbTable_Courses();
				$albums->updateCourse($id, $school, $dept, $cat, $courseNo, $courseName);
				$this->_forward('getcourses', 'courses', 'admin', array('school'=>$sid, 'dept'=>$deptid));
				} else {
				$form->populate($formData);
				}
				} else {
				$id = $this->_getParam('id', 0);
				if ($id > 0) {
				$albums = new Admin_Model_DbTable_Courses();
				$data = $albums->getCourse($id);
				print_r($data);
				$form->populate($data);
				}
				}
				}
		/**
		 * Pagination: Helper function
		 * @param unknown_type $resultset
		 * @param unknown_type $count
		 */
		private function pagination($resultset, $count){
        $paginator = new Zend_Paginator(new Zend_Paginator_Adapter_DbSelect($resultset));
        $paginator->setItemCountPerPage($count)	        
        		  ->setCurrentPageNumber($this->_getParam('page', 1));
        $this->view->paginator = $paginator;
		}

}

